//
//  IdVerfityViewController.m
//  citzenWorkStation
//
//  Created by yang qing on 2018/3/5.
//  Copyright © 2018年 yang qing. All rights reserved.
//

#import "IdVerfityViewController.h"
#import "UIColor+Hex.h"
#import "PickViewDialog.h"
#import "UserInfoBean.h"
#import "NSObject+YYModel.h"
#import "DataSaveUtils.h"
#import "CommonReqManage.h"
#import "ProtocolViewController.h"
@interface IdVerfityViewController ()
@property (weak, nonatomic) IBOutlet UITextField *tfIdNumber;
@property (weak, nonatomic) IBOutlet UITextField *tfName;
@property (weak, nonatomic) IBOutlet UITextField *tfSex;
@property (weak, nonatomic) IBOutlet UIImageView *ivCheck;
@property (weak, nonatomic) IBOutlet UIButton *btnVerfity;
@property (weak, nonatomic) IBOutlet UILabel *labelProtocol;
@property (strong,nonatomic) NSArray *datas;
@property (assign,nonatomic) BOOL agressDelegate;
@end

@implementation IdVerfityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _agressDelegate = NO;
    _datas =[[NSArray alloc] initWithObjects:@"男",@"女", nil];
    [self changeAgressStatus:_agressDelegate];
    [self initNav];
    [self initLisnter];
}

- (void)checkClick{
    _agressDelegate = !_agressDelegate;
    [self changeAgressStatus:_agressDelegate];
}

- (void)changeAgressStatus:(BOOL)status{
    if(status){
        [_ivCheck setImage:[UIImage imageNamed:@"icon_agreed"]];
    }else{
        [_ivCheck setImage:[UIImage imageNamed:@"icon_agreed_no"]];
    }
}

- (void)initLisnter{
    _ivCheck.userInteractionEnabled = YES;
    UIGestureRecognizer *checkTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(checkClick)];
    [_ivCheck addGestureRecognizer:checkTap];
    
    [_btnVerfity addTarget:self action:@selector(verfity) forControlEvents:UIControlEventTouchUpInside];
    
    
    _tfSex.userInteractionEnabled = YES;
     UIGestureRecognizer *sexTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sexClick)];
    [_tfSex addGestureRecognizer:sexTap];
    
    _labelProtocol.userInteractionEnabled = YES;
    [_labelProtocol addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(protocolClick)]];
    
    
    
}

- (void)sexClick{
    [self.view endEditing:YES];
    //弹出选择框
  PickViewDialog *pickDialog =  [[PickViewDialog alloc] initWithData:_datas title:@"请选择性别" select:^(PickViewDialog *pickViewDialog, NSInteger index, NSString *value) {
      _tfSex.text = value;
      [pickViewDialog hide];
    }];
    [pickDialog show];
}

- (void)protocolClick{
    ProtocolViewController *proVc = [[ProtocolViewController alloc] init];
    [self.navigationController pushViewController:proVc animated:YES];
}
- (void)verfity{
    if([self checkData]){
        UserInfoBean *userInfo = [DataSaveUtils getUserInfo];
        userInfo.identitynumber = _tfIdNumber.text;
        userInfo.name = _tfName.text;
        userInfo.sex = [_tfSex.text isEqualToString:@"男"]?1:2;
        [self showHub:@""];
        id userInfoStr = [userInfo yy_modelToJSONObject];
        [CommonReqManage identifyAuthentication:userInfoStr success:^(id success) {
               [self hiddenHub];
            NSString *status =  success[@"status"];
            if([status isEqualToString:@"1"]){
                [self showToast:@"已提交申请"];
                userInfo.auth = 3;
                [DataSaveUtils saveUserInfo:userInfo];
                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            }else{
            [self showTip:success[@"message"] cancelText:@"" confirmText:@"确定" Confirm:nil Cancel:nil];
            }
        } failed:^(id faiure) {
            [self hiddenHub];
            [self showHubAuto:@"请求失败"];
        }];
    }
}


- (BOOL)checkData{
   
    if([_tfIdNumber.text isEqualToString:@""]){
        [self showToast:@"请输入身份证号码"];
        return NO;
    }
    if([_tfName.text isEqualToString:@""]){
        [self showToast:@"请输入姓名"];
        return NO;
    }
    if([_tfSex.text isEqualToString:@""]){
        [self showToast:@"请选择性别"];
        return NO;
    }
    
    if(!_agressDelegate){
        [self showToast:@"请阅读并同意协议"];
        return NO;
    }
    
    return YES;
}

- (void)initNav{
    self.navigationController.navigationBar.barTintColor =
    [UIColor colorWithHexString:ThemeColor];
    //设置导航栏标题文字和文字大小
    [self setNavtitle:@"实名认证"];
    //设置两侧按钮
    [self setNavLeftImg:@"nav_back"];
    
    [self tfAddLeftImg:_tfIdNumber ImageName:@"icon_phone"];
    _tfIdNumber.keyboardType = UIKeyboardTypeNumberPad;
    [self tfAddLeftImg:_tfName ImageName:@"icon_code"];
    [self tfAddLeftImg:_tfSex ImageName:@"icon_pwd"];
    
}



@end
